package com.example.demo4_sport.service.impl;

import com.example.demo4_sport.common.ResultMapUtils;
import com.example.demo4_sport.entity.QueryInfoByPages;
import com.example.demo4_sport.entity.User;
import com.example.demo4_sport.mapper.UserMapper;
import com.example.demo4_sport.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public String getUserByMessage(User user) {
        final User user_info = userMapper.getUserByMessage(user.getUsername(), user.getPassword());
        System.out.println(user_info);
        if (user_info == null) {
            return ResultMapUtils.returnResult(null, "0");
        } else {
            System.out.println(ResultMapUtils.returnResult(user_info, "1"));
            return ResultMapUtils.returnResult(user_info, "1");
        }

    }


    @Override
    public String getAllUsersByPages(QueryInfoByPages queryInfo) {
        System.out.println(queryInfo.getQuery());
        // 获取到最大列表数和当前编号
        int numbers = getUserCounts(queryInfo.getQuery());
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        final List<User> allUsersByPages = userMapper.getAllUsersByPages(queryInfo.getQuery(), pageStart, queryInfo.getPageSize());
        System.out.println(allUsersByPages);
        return ResultMapUtils.returnAllUsersResult(numbers, allUsersByPages);
    }

    @Override
    public Integer getUserCounts(String username) {
        final Integer numbers = userMapper.getUserCounts(username);
        return numbers;
    }

    @Override
    public String changeUserState(Integer id, Boolean state) {

        try {
            Integer code = userMapper.changeUserState(id, state);
            return code > 0 ? ResultMapUtils.returnResult("修改成功", "1")
                    : ResultMapUtils.returnResult("修改失败", "1");
        }catch (Exception e){
            return ResultMapUtils.returnResult("数据库异常","2");
        }

    }

    @Override
    public String insertUser(User user) {
        user.setRole("普通管理员");
        user.setState(false);
        try{
            Integer code = userMapper.insertUser(user);
            return code > 0 ? ResultMapUtils.returnResult("创建成功", "1")
                    : ResultMapUtils.returnResult("创建失败", "1");
        } catch (DataAccessException e){
            return ResultMapUtils.returnResult(e.getCause(),"2");
        }
    }

    @Override
    public String deleteUserById(Integer id) {
        try{
            Integer code = userMapper.deleteUserById(id);
            System.out.println(code);
            return code > 0 ? ResultMapUtils.returnResult("删除成功", "1")
                    : ResultMapUtils.returnResult("删除失败", "2");
        } catch (DataAccessException e){
            return ResultMapUtils.returnResult(e.getCause(),"2");
        }
    }

    @Override
    public String getUpdateUserById(Integer id) {
        try{
            User user_info = userMapper.getUpdateUserById(id);
            System.out.println(user_info);
            return user_info != null ? ResultMapUtils.returnResult(user_info, "1")
                    : ResultMapUtils.returnResult("获取失败", "2");
        } catch (DataAccessException e){
            return ResultMapUtils.returnResult(e.getCause(),"2");
        }
    }

    @Override
    public String updateUserById(User user) {
        try {
            Integer code = userMapper.updateUserById(user);
            return code > 0 ? ResultMapUtils.returnResult("修改成功", "1")
                    : ResultMapUtils.returnResult("修改失败", "1");
        }catch (Exception e){
            return ResultMapUtils.returnResult("数据库异常","2");
        }
    }


}
